package com.servlet;

import com.bean.BhRole;
import com.bean.BhUser;
import com.bean.UserTopicKu;
import com.dao.BhSyCaseDao;
import com.dao.impl.BhSyCaseDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/UserBhSy/*")
public class BhSyCaseServlet extends BaseServlet {
    BhSyCaseDao bcd = new BhSyCaseDaoImpl();
    //------------------------------------------跳转---------------------------------------------
    public void openWeb(HttpServletRequest request, HttpServletResponse response){
        try {
            request.setCharacterEncoding("utf-8");
            String u = request.getParameter("findU");

            request.getRequestDispatcher("/WEB-INF/jsp/"+u).forward(request,response);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }
    //------------------------------------------登录---------------------------------------------
    public void Login(HttpServletRequest request,HttpServletResponse response){
        System.out.println("Login");
        HttpSession session = request.getSession();
        String account = request.getParameter("account");
        int pwd = Integer.parseInt(request.getParameter("pwd"));
        System.out.println(account);
        System.out.println(pwd);
        try {
            BhUser bh = bcd.bhLogin(account,pwd);
            System.out.println(bh);
            List<BhRole> br = bcd.bhSelRole(bh.getuSystem());
            System.out.println(br);
            session.setAttribute("BhSyUser",bh);
            session.setAttribute("BhSyRole",br);
            session.setAttribute("flagUlCase",0);
            if(session.getAttribute("kao")!=null) {
                response.sendRedirect((String) session.getAttribute("kao"));
                session.setAttribute("kao",null);
            } else {
                if (bh.getuSystem() == 1) {
                    request.getRequestDispatcher("/UserBhSy/student").forward(request, response);
                } else if (bh.getuSystem() == 2) {
                    request.getRequestDispatcher("/UserBhSy/teacher").forward(request, response);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    //---------------------------------老师和学生
    public void teacher(HttpServletRequest request,HttpServletResponse response){
        try {
            request.getRequestDispatcher("/WEB-INF/jsp/BhSyCaseTeacher.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void student(HttpServletRequest request,HttpServletResponse response){
        try {
            request.getRequestDispatcher("/WEB-INF/jsp/BhSyCaseStudent.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
